
package managedbean.CRM.SaleLead;

import entity.CRM.LineItem;
import entity.CRM.SaleInquiry;
import entity.CRM.SaleLead;
import entity.Employee;
import java.io.IOException;
import java.io.Serializable;
import java.util.List;
import javax.ejb.EJB;
import javax.el.ELContext;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;
import javax.faces.event.ActionEvent;
import javax.faces.event.PhaseEvent;
import managedbean.Controller;
import session.stateless.CRM.SaleInquiryBeanLocal;

@ManagedBean(name="viewSaleLeadsInquiries")
@ViewScoped
public class ViewSaleLeadsInquiries implements Serializable{

    public ViewSaleLeadsInquiries() {
    }
    
    @EJB
    private SaleInquiryBeanLocal saleInquiryBeanLocal;
    private SaleLead selectedSaleLead;
    private List<SaleInquiry> selectedSaleInquiries;
    private SaleInquiry selectedSaleInquiry;
    private List<LineItem> lineItems;
    private String statusMessage;
    
    public void initView(PhaseEvent event)
    {
        selectedSaleLead = (SaleLead)FacesContext.getCurrentInstance().getExternalContext().getFlash().get("selectedSaleLead");            
        selectedSaleInquiries = saleInquiryBeanLocal.getSaleInquiriesBySaleLead(selectedSaleLead.getRegistrationNumber());
    }

    public SaleLead getSelectedSaleLead() {
        return selectedSaleLead;
    }

    public void setSelectedSaleLead(SaleLead selectedSaleLead) {
        this.selectedSaleLead = selectedSaleLead;
    }

    public SaleInquiryBeanLocal getSaleInquiryBeanLocal() {
        return saleInquiryBeanLocal;
    }

    public void setSaleInquiryBeanLocal(SaleInquiryBeanLocal saleInquiryBeanLocal) {
        this.saleInquiryBeanLocal = saleInquiryBeanLocal;
    }

    public SaleInquiry getSelectedSaleInquiry() {
        return selectedSaleInquiry;
    }

    public void setSelectedSaleInquiry(SaleInquiry selectedSaleInquiry) {
        this.selectedSaleInquiry = selectedSaleInquiry;
    }
    
    public List<SaleInquiry> getSelectedSaleInquiries() {
        return selectedSaleInquiries;
    }

    public void setSelectedSaleInquiries(List<SaleInquiry> selectedSaleInquiries) {
        this.selectedSaleInquiries = selectedSaleInquiries;
    }

    public String getStatusMessage() {
        return statusMessage;
    }

    public void setStatusMessage(String statusMessage) {
        this.statusMessage = statusMessage;
    }

    public List<LineItem> getLineItems() {
        return lineItems;
    }

    public void setLineItems(List<LineItem> lineItems) {
        this.lineItems = lineItems;
    }
    
    public List<SaleInquiry> getSaleInquiry(){
        ELContext elContext = FacesContext.getCurrentInstance().getELContext();
        Controller controller = (Controller) FacesContext.getCurrentInstance().getApplication().getELResolver().getValue(elContext, null, "controller");
        Employee executive = controller.getEmployee();
        return saleInquiryBeanLocal.getSaleInquiries(executive.getEmployeeId());
    }
    
    public void editSaleInquiry(ActionEvent event) throws IOException
    {
        SaleInquiry selectedSaleInquiry = this.getSelectedSaleInquiry();
        FacesContext.getCurrentInstance().getExternalContext().getSessionMap().put("selectedSaleInquiry", selectedSaleInquiry);
        FacesContext.getCurrentInstance().getExternalContext().redirect("../saleinquiry/editSaleInquiry.xhtml");
    }
    
    public void deleteSaleInquiry(ActionEvent event) throws IOException
    {
        try
        {
            saleInquiryBeanLocal.deleteSaleInquiry(selectedSaleInquiry.getId());
            FacesContext.getCurrentInstance().getExternalContext().redirect("viewSaleLeadsInquiries.xhtml");
            statusMessage="Sale Inquiry Deleted Successfully.";
        }
        catch (Exception ex)
        {
            ex.printStackTrace();
        }                
    }
    
    public void convertToSaleQuotation(ActionEvent event) throws IOException
    {
        SaleInquiry selectedSaleInquiry = this.getSelectedSaleInquiry();
        try
        {
            FacesContext.getCurrentInstance().getExternalContext().getSessionMap().put("selectedSaleInquiry", selectedSaleInquiry);
            FacesContext.getCurrentInstance().getExternalContext().redirect("../salequotation/addQuotationFromInquiry.xhtml");
        }
        catch (Exception ex)
        {
            ex.printStackTrace();
        }
    }
}